package com.cecil.generate.domain;

import com.cecil.generate.constants.CommonConstants;
import com.cecil.generate.constants.GlobalProperties;
import com.cecil.generate.util.StringUtil;
import com.google.common.base.CaseFormat;
import lombok.Data;

import java.util.List;

@Data
public class Table {

    /**
     * 库名
     */
    private String tableSchema;
    /**
     * 表名
     */
    private String tableName;
    /**
     * 表名-变量名
     */
    private String varClassName;
    /**
     * 注释
     */
    private String tableComment;
    /**
     * java 类名
     */
    private String className;
    /**
     * controller请求路径
     */
    private String mappingUrl;
    /**
     * 字段
     */
    private List<Column> columns;

    public void updateProp(GlobalProperties globalProperties) {
        String tablePrefix = globalProperties.getTablePrefix();
        String name = tableName;
        if (StringUtil.isEmpty(tablePrefix) && tableName.startsWith(tablePrefix)) {
            name = tableName.substring(tableName.indexOf(tablePrefix) + tablePrefix.length());
        }
        this.className = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
        this.varClassName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
        this.mappingUrl = name.replace(CommonConstants.UNDERSCORE, CommonConstants.SLASH);
    }
}
